<template>
  <div class="container">
    <div class="main">
      <h2 class="title">登录</h2>
      <el-form :model="form" ref="formRef" :rules="rules" label-width="0">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" clearable>
            <template #prefix>
              <span style="color: #666;width:40px;">用户名</span>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="form.password" placeholder="密码" type="password" clearable>
            <template #prefix>
              <span style="color: #666;width:40px">密码</span>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-checkbox v-model="form.remember">记住密码</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>

        <el-form-item>
          <el-link type="info" @click="$router.push('/register')">没有账号？注册</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {useUserStore} from "@/stores/user.js";
import {useRouter} from "vue-router";
import {reactive, ref} from "vue";
import {ElMessage} from "element-plus";

const userStore = useUserStore()
const router = useRouter()


const formRef = ref(null)
const form = reactive({
  username: '',
  password: '',
  remember: false
})
const rules = {
  username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
  password: [{required: true, message: '请输入密码', trigger: 'blur'}],
}

const handleLogin = async () => {
  try {
    const valid = await formRef.value.validate()
    if (!valid) return

    await userStore.userLogin(
        {
          username: form.username,
          password: form.password
        },
        form.remember // 记住密码，存 localStorage
    )

    ElMessage.success(`欢迎回来，${form.username}!`)
    await router.push('/')
  } catch (err) {
    let errorMessage = '登录失败，请重试'
    if (err.response) {
      errorMessage = err.response.data.detail || err.response.data.message || errorMessage
    } else if (err.request) {
      errorMessage = '网络错误，请检查网络连接'
    }
    ElMessage.error(errorMessage)
    form.password = ''
  }
}

</script>

<style scoped>
.container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  background: linear-gradient(-45deg, #5caffd, #33e8d8, #696aff, #ae79ff);
  background-size: 400% 400%;
  animation: gradientBG 15s ease infinite;

}

.main {
  background-color: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
  width: 360px;
  margin-top: 10vh;
}

.title {
  text-align: center;
  margin: 0 0 30px;
  font-size: 22px;
  color: skyblue;
}

@keyframes gradientBG {
  0% {
    background-position: 0 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0 50%;
  }
}
</style>
